package com.linpq.road.service;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.linpq.common.core.domain.AjaxResult;
import com.linpq.common.core.domain.entity.SysDictData;
import com.linpq.common.core.domain.entity.SysDictType;
import com.linpq.common.core.service.CrudService;
        import com.linpq.common.utils.DateUtils;
import com.linpq.common.utils.StringUtils;
import com.linpq.road.domain.RdImportLog;
import com.linpq.road.domain.RdRoadSection;
import com.linpq.road.domain.RdRoadStake;
import com.linpq.road.mapper.RdImportLogMapper;
import com.linpq.road.mapper.RdRoadSectionMapper;
import com.linpq.road.mapper.RdRoadStakeMapper;
import com.linpq.system.mapper.SysDictDataMapper;
import com.linpq.system.mapper.SysDictTypeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import com.linpq.road.mapper.RdPavementMaterialsMapper;
import com.linpq.road.domain.RdPavementMaterials;
import com.linpq.common.core.text.Convert;
import com.linpq.common.core.service.BaseService;
import org.springframework.util.ObjectUtils;

/**
 * 路面材料Service业务层处理
 *
 * @author linpq
 * @date 2021-07-29
 */
@Service
public class RdPavementMaterialsService  extends CrudService<RdPavementMaterialsMapper, RdPavementMaterials> {

    private static final int STATE=0;
    @Autowired
    private RdPavementMaterialsMapper pavementMaterialsMapper;
    @Autowired
    private SysDictTypeMapper typeMapper;
    @Autowired
    private RdRoadStakeMapper rdRoadStakeMapper;
    @Autowired
    private RdRoadSectionMapper sectionMapper;
    @Autowired
    RdImportLogMapper rdImportLogMapper;
    @Autowired
    SysDictDataMapper dictDataMapper;


    /**
     * 查询路面材料
     *
     * @param id 路面材料ID
     * @return 路面材料
     */

    public RdPavementMaterials selectRdPavementMaterialsById(String id) {
        return dao.get(id);
    }

    /**
     * 查询路面材料列表
     *
     * @param rdPavementMaterials 路面材料
     * @return 路面材料
     */

    public List<RdPavementMaterials> selectRdPavementMaterialsList(RdPavementMaterials rdPavementMaterials) {
        return dao.findList(rdPavementMaterials);
    }

    /**
     * 新增路面材料
     *
     * @param rdPavementMaterials 路面材料
     * @return 结果
     */

    public int insertRdPavementMaterials(RdPavementMaterials rdPavementMaterials) {
        rdPavementMaterials.setCreateTime(DateUtils.getNowDate());
        return super.save(rdPavementMaterials);
    }

    /**
     * 修改路面材料
     *
     * @param rdPavementMaterials 路面材料
     * @return 结果
     */

    public int updateRdPavementMaterials(RdPavementMaterials rdPavementMaterials) {
                rdPavementMaterials.setUpdateTime(DateUtils.getNowDate());
        return super.save(rdPavementMaterials);
    }

    /**
     * 删除路面材料对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */

    public int deleteRdPavementMaterialsByIds(String ids) {
        return dao.deleteRdPavementMaterialsByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除路面材料信息
     *
     * @param id 路面材料ID
     * @return 结果
     */
    public int deleteRdPavementMaterialsById(String id) {
        return dao.delete(id);
    }

    /**
     * 已初始化路桩未初始化路面材料的路桩
     * @return
     */
    public Map<String,List<String>> selectRdPavementMaterialsNameList(){
        //面层材料组合列表
        List<String> stringArraySurfaceList = new ArrayList<>();
        //基层材料组合列表
        List<String> stringArrayBaseList = new ArrayList<>();
        //已初始化路转未初始化路面材料的路段列表
        List<String> stringArrayRoadList = new ArrayList<>();
        
        //更具锁来查询当前已经初始化的路段
        List<RdRoadSection> rdRoadSections = sectionMapper.selectRdRoadSectionByLocked();
        for (RdRoadSection rdRoadSection:rdRoadSections) {
            //标志位用于判断路面材料的对象是否存在
            boolean uninitialized=false;
            //获取已经初始化的路段在路面材料表判断当前路段的路面材料是否已经初始化
            List<RdPavementMaterials> rdPavementMaterials = pavementMaterialsMapper.selectRdPavemwntMaterialsNameList(rdRoadSection.getId());
            //如果这个路面材料的对象存在，则不需要将她加入我们的集合
            if (rdPavementMaterials.size()==0){
                uninitialized=true;
            }
            if (uninitialized){
                stringArrayRoadList.add(rdRoadSection.getSectionName());
            }
        }

        //面层材料组合
        SysDictType road_combinationOfSurfaceMaterial = typeMapper.selectDictTypeByType("road_combinationOfSurfaceMaterial");
        String roadCombinationOfSurfaceMaterialRemark = road_combinationOfSurfaceMaterial.getRemark();
        String[] split = roadCombinationOfSurfaceMaterialRemark.split(";");
        if (split != null) {
            for (String roadCombinationOfSurfaceMaterialSplit:split) {
                stringArraySurfaceList.add(roadCombinationOfSurfaceMaterialSplit);
            }
        }

        SysDictType road_combinationOfBaseMaterial = typeMapper.selectDictTypeByType("road_combinationOfBaseMaterial");
        String roadCombinationOfBaseMaterialRemark = road_combinationOfBaseMaterial.getRemark();
        String[] roadCombinationOfBaseMaterialRemarkSplit = roadCombinationOfBaseMaterialRemark.split(";");
        if (road_combinationOfBaseMaterial != null) {
            for (String roadCombinationOfBaseMaterialSplit:roadCombinationOfBaseMaterialRemarkSplit) {
                stringArrayBaseList.add(roadCombinationOfBaseMaterialSplit);
            }
        }
        Map<String, List<String>> stringListHashMap = new HashMap<>();
        stringListHashMap.put("stringArrayRoadList",stringArrayRoadList);
        stringListHashMap.put("stringArraySurfaceList",stringArraySurfaceList);
        stringListHashMap.put("stringArrayBaseList",stringArrayBaseList);
        return stringListHashMap;
    }

    /**
     * 插入路面材料信息
     * @param data
     * @return
     * @throws SQLException
     */
    public AjaxResult insertPavementMaterialsByRoadSectionName(Map<String ,String> data) throws SQLException {
        List<RdRoadStake> rdRoadStakes = new ArrayList<>();
        List<RdPavementMaterials> rdPavementMaterialsInfoList = new ArrayList<>();

        if (StringUtils.isNotEmpty(data.get("road"))&&!data.get("road").equals("")){
            //获取所有路转的序列号和id
            rdRoadStakes = rdRoadStakeMapper.selectRoadPavementMaterialByRoadSectionName(data.get("road"));
        }
        if (StringUtils.isNotEmpty(data.get("roadCombinationOfSurfaceMaterial"))&&StringUtils.isNotEmpty("roadCombinationOfBaseMaterial")){
            for (RdRoadStake roadStake:rdRoadStakes) {
                RdPavementMaterials rdPavementMaterials = new RdPavementMaterials();
                rdPavementMaterials.setFkId(roadStake.getRoadSection());
                rdPavementMaterials.setSerialNumber(roadStake.getSerialNumber());
                rdPavementMaterials.preInsert();
                rdPavementMaterials.setCombinationOfSurfaceMaterial(data.get("roadCombinationOfSurfaceMaterial"));
                rdPavementMaterials.setCombinationOfBaseMaterial(data.get("roadCombinationOfBaseMaterial"));
                rdPavementMaterialsInfoList.add(rdPavementMaterials);
            }
        }
        //路段名字得到对应的路桩路段编号
        //获取路桩的id
        //获取路桩的序列号
        int i = pavementMaterialsMapper.insertRdRoadPavementMaterialsList(rdPavementMaterialsInfoList);
        if (i==STATE){
            throw new SQLException();
        }
        updateRoadImgInfoByRoadSectionid(data.get("road"));
        return AjaxResult.success();
    }

    /**
     * 修改日志信息
     * @param id
     * @return
     */
    public int updateRoadImgInfoByRoadSectionid(String id){
        String roadType = dictDataMapper.selectDictDataValue("路面材料", "rd_data_type");
        if (StringUtils.isNotEmpty(id)&&!"".equals(id)){
            RdRoadSection byRoadName = sectionMapper.getByRoadName(id);
            RdImportLog rdImportLog = new RdImportLog();
            rdImportLog.setRoadSectionId(byRoadName.getId());
            rdImportLog.setImportType(roadType);
            rdImportLog.setResult(1);
            rdImportLog.setImportState(1);
            rdImportLogMapper.updateByRoadSectionid(rdImportLog);
        }
        return 0;
    }
}
